package top.zhdev.community.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import top.zhdev.community.entity.DiscussPost;
import top.zhdev.community.entity.Page;
import top.zhdev.community.entity.User;
import top.zhdev.community.service.DiscussPostService;
import top.zhdev.community.service.LikeService;
import top.zhdev.community.service.UserService;
import top.zhdev.community.utils.CommunityConstant;
import top.zhdev.community.utils.CookieUtil;
import top.zhdev.community.utils.HostHolder;
import top.zhdev.community.utils.RedisKeyUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController implements CommunityConstant {
    private DiscussPostService discussPostService;
    private UserService userService;
    private LikeService likeService;
    private HostHolder hostHolder;

    @Autowired
    public void setDiscussPostService(DiscussPostService discussPostService){
        this.discussPostService = discussPostService;
    }

    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    @Autowired
    public void setLikeService(LikeService likeService){
        this.likeService = likeService;
    }

    @Autowired
    public void setHostHolder(HostHolder hostHolder){
        this.hostHolder = hostHolder;
    }

    @GetMapping("/")
    public String root(){
        return "forward:/index";
    }

    @GetMapping("/index")
    public String getIndexPage(Model model, Page page, @RequestParam(name = "orderMode",
            defaultValue = "1") int orderMode, HttpServletRequest request){
        // 方法调用前，Springmvc会自动实例化Model，Page，并将page注入到model中
        // 所以thymeleaf中可以直接访问page中的对象数据
        // orderMode 是首页的最热：1，最新：0
        page.setRows(discussPostService.getDiscussPostRows(0));
        page.setPath("/index?orderMode="+orderMode);
        List<DiscussPost> list = discussPostService.getDiscussPosts(0,page.getOffset(),page.getLimit(),orderMode);
        List<Map<String,Object>> discussPosts = new ArrayList<>();
        if(list != null){
            for (DiscussPost post : list){
                Map<String,Object> map = new HashMap<>();
                // 帖子
                map.put("post",post);
                // 用户信息
                User user = userService.getUserById(post.getUserId());
                map.put("user",user);

                // 点赞数量
                long likeCount = likeService.getEntityLikeCount(ENTITY_TYPE_POST,post.getId());
                map.put("likeCount",likeCount);

                discussPosts.add(map);
            }
        }

        model.addAttribute("discussPosts",discussPosts);
        model.addAttribute("orderMode",orderMode);
        model.addAttribute("page",page);

        return "index";
    }
}
